package uk.ac.ebi.bioinvindex.model.impl.term;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Store;
import uk.ac.ebi.bioinvindex.model.impl.AnnotatableImpl;
import uk.ac.ebi.bioinvindex.model.impl.xref.ReferenceSourceImpl;

import javax.persistence.*;

/**
 * User: Nataliya Sklyar (nsklyar@ebi.ac.uk)
 * Date: Jul 12, 2007
 */


@Entity
@Table(name = "ontology_entry")
@Inheritance (strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "obj_type", discriminatorType = DiscriminatorType.STRING, length = 255)
public abstract class OntologyEntryImpl extends AnnotatableImpl {

	@Field(index = Index.TOKENIZED, store = Store.YES)
	protected String name;

	protected ReferenceSourceImpl source;

	protected OntologyEntryImpl() {
		super();
	}

	protected OntologyEntryImpl(String name, ReferenceSourceImpl source) {
		this.name = name;
		this.source = source;
	}

	public OntologyEntryImpl(String acc, String name, ReferenceSourceImpl source ) {
		this.acc = acc;
		this.name = name;
		this.source = source;
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne( targetEntity = uk.ac.ebi.bioinvindex.model.impl.xref.ReferenceSourceImpl.class)
	@JoinColumn(name = "source", nullable = false)
	public ReferenceSourceImpl getSource() {
		return this.source;
	}

	public void setSource(ReferenceSourceImpl source) {
		this.source = source;
	}

	/**
	 * Returns true if ACC, ReferenceSource and ReferenceSource.acc are specified
	 * @return
	 */
	@Transient
	public boolean isValid() {
		return acc != null && source != null && source.getAcc() != null;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (!(o instanceof OntologyEntryImpl)) return false;
		if (!super.equals(o)) return false;

		OntologyEntryImpl that = (OntologyEntryImpl) o;

		if (!name.equals(that.name)) return false;
		if (!source.equals(that.source)) return false;

		return true;
	}

	@Override
	public int hashCode() {
		int result = super.hashCode();
		result = 31 * result + name.hashCode();
		result = 31 * result + source.hashCode();
		return result;
	}

	@Override
	public String toString() {
		return "OntologyEntryImpl{" +
		    " id = '" + this.getId () + '\'' +
		    ", accession= '" + this.getAcc () + '\'' +
				", name='" + getName () + '\'' +
				", source=" + getSource () +
				'}';
	}
}
